<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\wechat\controller\admin;

use app\admin\common\AdminController;

class Mp extends AdminController
{
    public function index()
    {
        $data = $this->_get_pagination_data();

        if ($data['search_word']) {
            $where_list[] = "(id = :search_word OR name LIKE CONCAT('%', :search_word, '%'))";
        }

        $where_sql = isset($where_list) ? ' WHERE ' . implode(' AND ', $where_list) : '';
        $where_bind = $data['where_map'] + array('search_word' => $data['search_word']);

        $count_sql = 'SELECT COUNT(1) FROM {wechat_mp}' . $where_sql;
        $data['total_rows'] = intval(helper('admin/sql')->query_value($count_sql, $where_bind));
        $data['page_count'] = intval(ceil($data['total_rows'] / $data['page_size']));

        $select_sql = 'SELECT * FROM {wechat_mp}' . $where_sql;
        $data['mp_list'] = helper('admin/sql')->query_list($select_sql, $where_bind);

        $mp_type_list = service('wechat/mp')->get_mp_type_list();

        foreach ($data['mp_list'] as &$row) {
            $row['_type'] = isset($mp_type_list[$row['type']]) ? $mp_type_list[$row['type']] : '';
        }

        $this->display('admin/mp/mp_list', $data);
    }

    public function create()
    {
        $mp_info = model('wechat/mp')->initDefault();

        if (request()->isPost()) {
            $mp_info->acceptData($this->post_array());

            if ($mp_info->save() !== false) {
                $this->success('新增成功。', $this->url('index'));
            }
        }

        $data['mp_info'] = $mp_info;
        $data['mp_type_list'] = service('wechat/wx')->get_type_list();
        $this->display('admin/mp/mp_form', $data);
    }

    public function modify($id)
    {
        $mp_info = model('wechat/mp')->findOrFail($id);

        if (request()->isPost()) {
            $mp_info->acceptData($this->post_array());

            if ($mp_info->save() !== false) {
                $this->success('修改成功。', $this->url('index'));
            }
        }

        $data['mp_info'] = $mp_info;
        $data['mp_type_list'] = service('wechat/mp')->get_mp_type_list();
        $this->display('admin/mp/mp_form', $data);
    }

    public function bulk_delete($ids = array())
    {
        $mp_list = model('wechat/mp')->where('id', 'in', $ids)->select();

        foreach ($mp_list as $mp_info) {
            $mp_info->delete();
        }

        $this->success('删除成功。', $this->url('index'));
    }
}